import { getUserDetail, getUserInfo, login } from '@/api/user'
import { setToken, getToken, removeToken } from '@/utils/auth'

const state = {
  token: getToken(),
  userInfo: {}
}
const mutations = {
  setToken(state, token) {
    state.token = token
    setToken(token)
  },
  removeToken(state) {
    state.token = ''
    removeToken()
  },
  setUserInfo(state, payload) {
    state.userInfo = payload
  }
}
const actions = {
  async login(context, payload) {
    const data = await login(payload)
    context.commit('setToken', data)
  },
  async getUserInfo(context) {
    const data = await getUserInfo()
    const data2 = await getUserDetail(data.userId)
    context.commit('setUserInfo', { ...data, ...data2 })
  },
  logout(context) {
    context.commit('removeToken')
    context.commit('removeUserInfo')
  }
}
const getters = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}

